home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / textual / tex / files / !preview / GFtoXY / c / raster < prev   
Encoding:
Text File  |  1990-07-26  |  8.2 KB  |  395 lines

  1. /* raster.c TIGGR */
  2.  
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. /* Header of a f9999x9999 file.  The DESCR_BEGIN field marks the start of the
  9.    textual description of of the font.  It is included to make the size of
  10.    the struct a multiple of 32bits.  */
  11.  
  12. struct font_header
  13. {
  14.   int magic;                                      /* "FONT"  */
  15.   unsigned int bpp:8, version:8, flags:16;        /* 4, 4, 0 */
  16.   int x0:16, y0:16, w:16, h:16;                   /* font bb */
  17.   unsigned int ch_offset[9];                      /*         */
  18.   unsigned int n:16, x_size:16, x_res:16, y_size:16, y_res:16, descr_begin:16;
  19. };
  20.  
  21.  
  22. struct fontdata
  23. {
  24.   int xds, yds, xdpi, ydpi;
  25.   signed char x0, y0, x1, y1;
  26.   unsigned short offsets[256];
  27. };
  28.  
  29.  
  30. /* Header of IntMetrics file.  */
  31.  
  32. struct intmetrics_header
  33. {
  34.   char f_name[40];
  35.   int  sixteens[2];
  36.   char num_chars[4];   /* only nc[0] should be defined.  */
  37.   char mapping[256];
  38. };
  39.  
  40.  
  41. int bb[256][4];
  42. int bbw[256];
  43. char xoff[256], yoff[256];
  44.  
  45. int *pixmap[256];
  46. int horpixsize, *curmap;
  47. int numchars = 0;
  48. int dpi;
  49. int minx = 1 << 30, miny = 1 << 30, maxx = - (1 << 30), maxy = - (1 << 30);
  50. char *dirname;
  51. extern int designsize;
  52.  
  53. void
  54. fatal (char *fmt, ...)
  55. {
  56.  va_list ap;
  57.  
  58.   va_start (ap, fmt);
  59.   vfprintf (stderr, fmt, ap);
  60.   fprintf (stderr, "\n");
  61.   va_end (ap);
  62.   exit (1);
  63. }
  64.  
  65.  
  66. /* NAME is something like `font.300GF'.  */
  67.  
  68. void
  69. make_sprites (char *name)
  70. {
  71.   char *s = strrchr (name, '.'), *t;
  72.  
  73.   dpi = atoi (&s[1]);
  74.  
  75.   if (s == NULL)
  76.     {
  77.       dirname = malloc (1 + strlen (name));
  78.       strcpy (dirname, name);
  79.     }
  80.   else
  81.     {
  82.       dirname = malloc ((s - name) + 1);
  83.       strncpy (dirname, name, s - name);
  84.       dirname[s - name] = '\0';
  85.     }
  86. }
  87.  
  88.  
  89. void
  90. select_char2 (int chno, int w, int px)
  91. {
  92.   bbw[chno] = ((w * 250) / (designsize / 16));
  93.  
  94.   fprintf(stderr, "\n%d\n", bbw[chno]);
  95. }
  96.  
  97. void
  98. select_char (int chno, int xl, int xr, int yb, int yt)
  99. {
  100.   int w, h;
  101.  
  102.   fprintf(stderr, "select_char(c=%d xl=%d,yb=%d, xr=%d,yt=%d)\n", chno, xl,yb, xr,yt);
  103.  
  104.   bb[chno][0] = xl;
  105.   bb[chno][1] = yb;
  106.   bb[chno][2] = xr;
  107.   bb[chno][3] = yt;
  108.   if (xl < minx) minx = xl;
  109.   if (xr > maxx) maxx = xr;
  110.   if (yb < miny) miny = yb;
  111.   if (yt > maxy) maxy = yt;
  112.  
  113.   h = yt - yb + 1;
  114.   w = (31 + xr - xl) & ~31;
  115.   horpixsize = w / 32;
  116.   if (!horpixsize)
  117.     horpixsize = 1;
  118.  
  119.   if (pixmap[chno])
  120.     fatal ("char %d already handled?!?", chno);
  121.  
  122.   curmap = pixmap[chno] = calloc (1, h * horpixsize * 4);
  123.   if (curmap == NULL)
  124.     fatal ("Out of VM (char-card=%d, rw=%d, h=%d)", numchars, horpixsize * 32, h);
  125.   numchars++;
  126. } /* select_char */
  127.  
  128.  
  129.  
  130. void
  131. set_pixel (int row, int col)
  132. {
  133.   curmap[row * horpixsize + (col / 32)] |= 1 << (col & 31);
  134. }
  135.  
  136.  
  137.  
  138. void
  139. dump_char (void)
  140. {
  141. }
  142.  
  143.  
  144. void
  145. begin_dump_sprites (void)
  146. {
  147. }
  148.  
  149.  
  150. void
  151. write_nibble (int value, int *ff, int *state, FILE *f)
  152. {
  153.   register int q;
  154.  
  155.   if (*state == 0)
  156.     {
  157.       *ff = value & 0xf;
  158.       *state = 1;
  159.     }
  160.   else
  161.     {
  162.       q = *ff | ((value & 0xf) << 4);
  163.       fputc (q, f);
  164.       *state = 0;
  165.     }
  166. }
  167.  
  168.  
  169. void
  170. end_dump_sprites (void)
  171. {
  172.   FILE *f, *f_im;
  173.   char s[1024], s2[1024], s_im[1024];
  174.   int i, h, j, w, x, y, pos, l;
  175.   struct font_header hdr;
  176.   struct intmetrics_header im;
  177.   int fudge_pos = (char *) &hdr.ch_offset[0] - (char *) &hdr;
  178.   int offsets[32];
  179.   int nibble_state, nibble;
  180.  
  181.   memset (&hdr, 0, sizeof (struct font_header));
  182.   memset (&im, 0, sizeof (struct intmetrics_header));
  183.  
  184.   memset (&im.f_name, 13, sizeof (im.f_name));
  185.   sprintf (im.f_name, "%s", dirname);
  186.   im.f_name[strlen (dirname)] = 13;
  187.  
  188.   im.sixteens[0] = im.sixteens[1] = 16;
  189.   im.num_chars[0] = numchars + 1;
  190.  
  191.   strncpy ((char *) &hdr.magic, "FONT", 4);
  192.   hdr.bpp = 4;
  193.   hdr.version = 4;
  194.   hdr.flags = 0;
  195.   hdr.x0 = minx;
  196.   hdr.y0 = miny;
  197.   hdr.w = maxx - minx + 1;
  198.   hdr.w = maxy - miny + 1;
  199.   hdr.n = 10;
  200.   hdr.descr_begin = dirname[0] | (dirname[1] << 8);
  201.  
  202.   fprintf (stderr, "Begin writeout of font\nnumchars = %d\n", numchars);
  203.  
  204.   hdr.x_size = hdr.y_size = designsize >> 16;
  205.   fprintf (stderr, "size = %dpt\n", hdr.y_size / 16);
  206.   fprintf (stderr, "dpi = %d\n", dpi);
  207.  
  208.   if (dpi)
  209.     hdr.x_res = hdr.y_res = dpi;
  210.   else
  211.     {
  212.       fprintf (stdout, "dpi? ");
  213.       fflush (stdout);
  214.       fscanf (stdin, "%s", s);
  215.       hdr.x_res = hdr.y_res = atoi (s);
  216.       if (hdr.x_res == 0)
  217.         fatal ("dpi can't be 0");
  218.     }
  219.  
  220.   i = hdr.x_size * hdr.x_res / 72;
  221.   sprintf (s, "%s.f%dx%d", dirname, i, i);
  222.   sprintf (s2, "%s.x90y45", dirname);
  223.   sprintf (s_im, "%s.IntMetrics", dirname);
  224.  
  225.   f = fopen (s2, "w");
  226.   if (f == NULL)
  227.     fatal ("Can't open file `%s'", s2);
  228.   fprintf (f, "f%dx%d", i, i);
  229.   fclose (f);
  230.  
  231.   f_im = fopen (s_im, "wb");
  232.   if (f_im == NULL)
  233.     fatal ("Can't open file `%s'", s_im);
  234.  
  235.   f = fopen (s, "wb");
  236.   if (!f)
  237.     fatal ("Can't open file `%s'", s);
  238.   if (strlen (dirname) < 2)
  239.     fatal ("bogus fontname"); /* To make sure hdr.descr_begin and following stuff is correct.  */
  240.  
  241.   fwrite (&hdr, 1, sizeof (hdr), f);
  242.   fprintf (f, "%s", &dirname[2]);
  243.   fputc (0, f);
  244.   fprintf (f, "%dx%d point at %dx%d dpi", hdr.x_size / 16, hdr.y_size / 16, hdr.x_res, hdr.y_res);
  245.   fputc (0, f);
  246.   while (ftell (f) % 4)
  247.     fputc (0, f);
  248.  
  249.   /* All characters should have the top bit set.  */
  250.  
  251.   for (i = 0; i < 128; i++)
  252.     {
  253.       if (pixmap[128 + i])
  254.         fatal ("Can't shuffle characters (char=%d)", i);
  255.       pixmap[128 + i] = pixmap[i];
  256.       pixmap[i] = 0;
  257.       bbw[128 + i] = bbw[i];
  258.       for (j = 0; j < 4; j++)
  259.         bb[128 + i][j] = bb[i][j];
  260.     }
  261.  
  262.   fprintf (stderr, "Writing bitmaps... ");
  263.   fflush (stderr);
  264.  
  265.   for (i = 0; i < 9; i++)
  266.     {
  267.       /* POS holds the current EOF, and the start of this chunk.  It will be
  268.          used to search back to this spot because of the character offsets.  */
  269.  
  270.       pos = ftell (f);
  271.       fseek (f, fudge_pos + 4 * i, SEEK_SET);
  272.       fwrite (&pos, 1, 4, f);
  273.       fseek (f, pos, SEEK_SET);
  274.  
  275.       /* If the offset written was last exit, or if the corresponding chunks
  276.          was empty continue with next chunk.  */
  277.  
  278.       if (i == 8)
  279.         break;
  280.  
  281.       for (j = 0, l = 0; j < 32; j++)
  282.         l = l || pixmap[i * 32 + j];
  283.       if (!l)
  284.         continue;
  285.  
  286.       memset (offsets, 0, sizeof (offsets));
  287.       fwrite (offsets, 4, 32, f);
  288.  
  289.       for (j = 0; j < 32; j++)
  290.         if (pixmap[j + i * 32])
  291.           {
  292.             fprintf (stderr, "%d ", i * 32 + j);
  293.             fflush (stderr);
  294.  
  295.             offsets[j] = ftell (f) - pos;
  296.             fputc (0, f);
  297.             fputc (bb[i * 32 + j][0], f);
  298.             fputc (bb[i * 32 + j][1], f);
  299.             w = bb[i * 32 + j][2] - bb[i * 32 + j][0];
  300.             h = bb[i * 32 + j][3] - bb[i * 32 + j][1] + 1;
  301.             fputc (w, f);
  302.             fputc (h, f);
  303.  
  304.             nibble_state = 0;
  305.             horpixsize = ((w  + 31) & ~31)/ 32;
  306.             for (y = h - 1; y >= 0; y--)
  307.               for (x = 0; x < w; x++)
  308.                 write_nibble (pixmap[i * 32 + j][y * horpixsize + (x / 32)] & (1 << (x & 31)) ? 15 : 0, &nibble, &nibble_state, f);
  309.             if (nibble_state)
  310.               write_nibble (0, &nibble, &nibble_state, f);
  311.           }
  312.       while (ftell (f) % 4)
  313.         fputc (nibble_state, f);
  314.  
  315.       l = ftell (f);
  316.       fseek (f, pos, SEEK_SET);
  317.       fwrite (offsets, 4, 32, f);
  318.       fseek (f, l, SEEK_SET);
  319.     }
  320.  
  321.   fclose (f);
  322.  
  323.   fprintf (stderr, "\nWriting metrix... ");
  324.   fflush (stderr);
  325.  
  326.   for (i = 0, j = 1; i < 256; i++)
  327.     if (pixmap[i])
  328.       im.mapping[i] = j++;
  329.  
  330.   /* header + character mapping */
  331.   fwrite (&im, 1, sizeof (struct intmetrics_header), f_im);
  332.  
  333.  
  334. #define PATCH fputc (0, f_im); fputc (0, f_im)
  335.  
  336.  
  337.   /* x0 y0 x1 y1 */
  338.   for (j = 0; j < 4; j++)
  339. {PATCH;
  340.     for (i = 0; i < 256; i++)
  341.       if (pixmap[i])
  342.         {
  343.           l = (int) ((double) bb[i][j] / (double) dpi * 72.0 / ((double) designsize / (16.0 * 65536.0)) * 1000.0);
  344.  
  345.  
  346. /*
  347.  
  348. l = pixels
  349.  
  350.  / pixels-per-inch
  351.  
  352.  ->  inches
  353.  
  354.  * points per inch
  355.  
  356.  -> points
  357.  
  358.  / (16 * 64K points per em)
  359.  
  360.  -> em
  361.  
  362.  * 1000
  363.  
  364.  -> thousandths of an em
  365.  
  366.  
  367. */
  368.  
  369.  
  370. fprintf (stderr, "%c[%d]: %d\n", i, j, l);
  371.  
  372.           fwrite (&l, 1, 2, f_im);       /* SYSDEP */
  373.         }
  374.     }
  375.  
  376. PATCH;
  377.  
  378.   /* x offset */
  379.   for (i = 0; i < 256; i++)
  380.     if (pixmap[i])
  381.       {
  382.         l = bbw[i];
  383.         fwrite (&l, 1, 2, f_im);              /* SYSDEP */
  384.       }
  385.  
  386. PATCH;
  387.   
  388.   /* y offset */
  389.   for (i = 0, j = 0; i < 256; i++)
  390.     if (pixmap[i])
  391.       fwrite (&j, 1, 2, f_im);
  392.  
  393.   fprintf (stderr, " DONE!\n");
  394. }
  395.